<?php
defined('IN_IA') or exit('Access Denied');
class nbs_scdyModuleSite extends WeModuleSite 
{
	public function doMobileIndex() 
	{
		global $_W, $_GPC;
		load()->model('mc');
		$cfg = $this->module['config'];
		$fans=mc_oauth_userinfo();
		$mc = mc_fetch($fans['openid']);
		if(empty($cfg['lxtype']))
		{
			$credit='credit1';
		}
		else
		{
			$credit='credit2';
		}
		if(empty($mc[$credit]))
		{
			$hadou=0;
		}
		else
		{
			$hadou=intval($mc[$credit]);
		}
		if(empty($fans['openid']))
		{
			echo '&#26032;&#30591;&#31038;&#21306;&#25552;&#31034;&#65306;&#35831;&#22312;&#24494;&#20449;&#20013;&#25171;&#24320;&#65281;';
			exit;
		}
		$today = strtotime(date('Y-m-d', time()));
		$cycount=pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($this->modulename.'_jl')." where weid='{$_W['uniacid']}
	' and openid='{$fans['openid']}
' and createtime>='{$today}
'");
$cy=$cfg['jhsum']-$cycount;
if($cy<=0)
{
$cy=0;
}
include $this->template ( 'index' );
}
public function doMobileMember() 
{
global $_W, $_GPC;
load()->model('mc');
$cfg = $this->module['config'];
$fans=mc_oauth_userinfo();
$mc = mc_fetch($fans['openid']);
include $this->template ( 'member' );
}
public function doMobileHdget()
{
global $_W, $_GPC;
$cfg = $this->module['config'];
// file_put_contents(IA_ROOT."/addons/nbs_scdy/log.txt","\n old:".json_encode($_GPC),FILE_APPEND);
if(!$_W['isajax'])die(json_encode(array('success'=>0,'msg'=>'非法提交,只能通过网站提交')));
$weid=$_W['uniacid'];
$fans=mc_oauth_userinfo();
$mc = mc_fetch($fans['openid']);

if(empty($cfg['lxtype']))
{
$credit='credit1';
$msg="三锤定音扣积分";
}
else
{
$credit='credit2';
$msg="三锤定音扣余额";
}

mc_credit_update($mc['uid'],$credit,-$cfg['jf'],array($mc['uid'],$msg));
$cjdata=array( 'weid'=>$_W['uniacid'], 'openid'=>$fans['openid'], 'msg'=>$msg, 'createtime'=>TIMESTAMP, );
pdo_insert($this->modulename . "_jl", $cjdata);
$today = strtotime(date('Y-m-d', time()));
$cycount=pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($this->modulename.'_jl')." where weid='{$_W['uniacid']}
' and openid='{$fans['openid']}
' and createtime>='{$today}
'");
// $cy=$cfg['jhsum']-$cycount;
// if($cy<=0)
// {
// $cy=0;
// }
$url=$cfg['sburl'];
a:
$numarray=array($cfg['ji'],$cfg['ji2'],$cfg['ji3']);
$numresult = $this->get_rand($numarray);
// file_put_contents(IA_ROOT."/addons/nbs_scdy/zj.txt","\n old:".$zj,FILE_APPEND);
file_put_contents(IA_ROOT."/addons/nbs_scdy/zj.txt","\n old:".$numresult,FILE_APPEND);
if($numresult==1)
{

	//两个相同中奖设置
$wzj=$this->getNumGroups();
$bw=$wzj[0];
$gw=$wzj[2];
$sw=$wzj[1];
if ($this->issameid($wzj)) {
		if($cfg['jl4']!=0&&$cfg['jl4']!=null){
			$prize=$cfg['jl4'];
			die(json_encode(array("status"=>'ok',"hadou"=>intval($mc[$credit]),"awards"=>"nowing","bw"=>$bw,"sw"=>$sw,"gw"=>$gw,"prize"=>$prize,"chance"=>$cy,"url"=>$url,"msg"=>'网络出错,请稍候在试！')));
		}
}else{
	goto a;
}
// die(json_encode(array("status"=>'ok',"hadou"=>intval($mc[$credit]),"bw"=>$bw,"awards"=>"nowing","sw"=>$sw,"gw"=>$gw,"chance"=>$cy,"url"=>$url,"msg"=>'网络出错,请稍候在试！')));
}elseif($numresult==2){
	//三个不同不中奖设置
	$wzj=$this->getNumGroups();
	$bw=$wzj[0];
	$gw=$wzj[2];
	$sw=$wzj[1];
	if (!$this->issameid($wzj)) {
		die(json_encode(array("status"=>'ok',"hadou"=>intval($mc[$credit]),"bw"=>$bw,"awards"=>"nowing","sw"=>$sw,"gw"=>$gw,"chance"=>$cy,"url"=>$url,"msg"=>'网络出错,请稍候在试！')));
	}else{
		goto a;
	}
}
elseif(empty($numresult))
{
$sj=rand(0,2);
$bw=$sj;
$sw=$sj;
$gw=$sj;
if($sj==0)
{
$prize=$cfg['jl1'];
}
elseif($sj==1)
{
$prize=$cfg['jl2'];
}
elseif($sj==2)
{
$prize=$cfg['jl3'];
}
mc_credit_update($mc['uid'],$credit,$prize,array($mc['uid'],'三锤定音奖励'));
$data=array( 'weid'=>$_W['uniacid'], 'openid'=>$fans['openid'], 'nickname'=>$fans['nickname'], 'avatar'=>$fans['avatar'], 'msg'=>$msg, 'createtime'=>TIMESTAMP, );
if(empty($cfg['lxtype']))
{
$data['credit1']=$prize;
$data['credit2']=0;
}
else
{
$data['credit1']=0;
$data['credit2']=$prize;
}
pdo_insert($this->modulename . "_zjjl", $data);
$mc = mc_fetch($fans['openid']);
die(json_encode(array("status"=>'ok',"hadou"=>intval($mc[$credit]),"awards"=>"nowing","bw"=>$bw,"sw"=>$sw,"gw"=>$gw,"prize"=>$prize,"chance"=>$cy,"url"=>$url,"msg"=>'网络出错,请稍候在试！')));
}
}
function issameid($a){
	sort($a);
	if($a[2]-$a[1]==3||$a[1]-$a[0]==3||$a[2]-$a[0]==3){
		if($a[2]!=$a[1]&&$a[1]!=$a[0]&&$a[2]!=$a[0]){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}
function get_rand($proArr) {
	$result = '';
	foreach ($proArr as $key => $proCur) {
		if($proCur==0||$proCur==null){
			unset($proArr[$key]);
		}
	}
	
	//概率数组的总概率精度
	$proSum = array_sum($proArr);
	
	
	//概率数组循环
	foreach ($proArr as $key => $proCur) {
		$randNum = mt_rand(1, $proSum);
		if ($randNum <= $proCur) {
			$result = $key;
			break;
		} else {
			$proSum -= $proCur;
		}
	}
	unset ($proArr);
	return $result;
}
function sjs($a,$b)
{
$rands = rand(0,$a);
if($rands<=$b)
{
$num=1;
}
else
{
$num=0;
}
return $num;
}
public function doMobileCs()
{
$cfg = $this->module['config'];
$aa=$this->getNumGroups(3,3);
echo $zj=$this->sjs($cfg['qian'],$cfg['ji']);
echo '<br>';
$bb=$this->aa();
print_r($bb);
}
//五个序号随机拿出3个无规则排序
public function getNumGroups()
{
$arr = array(0,1,2,3,4);
$str = array();
for($i=0;$i<3;$i++)
{
$index = rand(0,count($arr)-1);
while(!isset($arr[$index]))
{
$index = rand(0,count($arr)-1);
}
$str[]=$arr[$index];
unset($arr[$index]);
}

return $str;
}

public function doWebZjjr() 
{
global $_W, $_GPC;
$pindex = max(1, intval($_GPC['page']));
$psize = 12;
if($_GPC['name'])
{
$name=$_GPC['name'];
$where="nickname like '%{$name}
%' )";
}
$list = pdo_fetchall ( 'select * from ' . tablename ( $this->modulename . "_zjjl" ) . " where weid='{$_W['uniacid']}
' {$where}
order by id desc LIMIT " . ($pindex - 1) * $psize . ",{$psize}
");
$total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename($this->modulename.'_zjjl')." where weid='{$_W['uniacid']}
' {$where}
");
$pager = pagination($total, $pindex, $psize);
include $this->template ( 'zjjl' );
}
}
?>